package com.lc.qianrenshiye.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.BaseApplication;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.BussnessCenterInfoPost;
import com.lc.qianrenshiye.dialog.LogoutDialog;
import com.lc.qianrenshiye.uitls.MyAnimationUitls;
import com.lc.qianrenshiye.uitls.MyToast;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

/**
 * Created by shenyuxin on 2016/11/16.
 */
public class BusinessCenterActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.business_center_rl) private RelativeLayout business_center_rl;
    //我的收益
    @BoundView(R.id.center_my_income_rl) private RelativeLayout center_my_income_rl;
    @BoundView(R.id.center_accrued_income_tv) private TextView center_accrued_income_tv;
    @BoundView(R.id.center_today_income_tv) private TextView center_today_income_tv;

     //设置
    @BoundView(R.id.center_setting_rl) private RelativeLayout center_setting_rl;

    //我的成本
    @BoundView(R.id.center_my_cast_rl) private RelativeLayout center_my_cast_rl;
    @BoundView(R.id.center_today_cast_tv) private TextView center_today_cast_tv;

    //我的订单
    @BoundView(R.id.center_my_order_rl) private RelativeLayout center_my_order_rl;
    @BoundView(R.id.center_today_not_pay_tv) private TextView center_today_not_pay_tv;
    @BoundView(R.id.center_today_payed_tv) private TextView center_today_payed_tv;

    //消息通知
    @BoundView(R.id.center_message_notify_rl) private RelativeLayout center_message_notify_rl;
    @BoundView(R.id.center_message_red_iv) private ImageView center_message_red_iv;

    //会员特权
    @BoundView(R.id.center_vip_privileges_rl) private RelativeLayout center_vip_privileges_rl;

    //退出登录
    @BoundView(R.id.center_logout_rl) private RelativeLayout center_logout_rl;

    private String shopName;

    private BussnessCenterInfoPost bussnessCenterInfoPost = new BussnessCenterInfoPost(new AsyCallBack<BussnessCenterInfoPost.BussnessCenterInfoEntity>() {
        @Override
        public void onSuccess(String toast, int type, BussnessCenterInfoPost.BussnessCenterInfoEntity bussnessCenterInfoEntity) throws Exception {

            shopName = bussnessCenterInfoEntity.title;

            RefreshTitleAndHead refreshTitleAndHead =  new RefreshTitleAndHead();

            //退出时保存的用户id 与新登录的用户id 不相等时候走判断方法
            if (!BaseApplication.BasePreferences.readUserId().equals(BaseApplication.BasePreferences.readLogoutUserId())) {

                refreshTitleAndHead.refreshMessage(bussnessCenterInfoEntity.news);
            }

            refreshTitleAndHead.refreshAll(bussnessCenterInfoEntity.title, bussnessCenterInfoEntity.picurl);


            center_accrued_income_tv.setText(bussnessCenterInfoEntity.gains);//预估收益
            center_today_income_tv.setText(bussnessCenterInfoEntity.todaygains);//今日收益
            center_today_cast_tv.setText(bussnessCenterInfoEntity.cost);//成本
            center_today_not_pay_tv.setText(bussnessCenterInfoEntity.amount);//未付款
            center_today_payed_tv.setText(bussnessCenterInfoEntity.money);//已付款

            MyAnimationUitls.toVisibleAnim(business_center_rl);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_center);

        setBackTrue();

        setTitleName(getString(R.string.business_center_title_text));

        bussnessCenterInfoPost.sid = BaseApplication.BasePreferences.readShopId();
        bussnessCenterInfoPost.execute(this);

        setAppCallBack(new RefreshTitleAndHead());

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.title_right_rl:

                Bundle bundle = new Bundle();

                bundle.putString("shopName", shopName);

                startActivity(new Intent(this, UserInfoActivity.class).putExtras(bundle));

                break;
            case R.id.center_my_income_rl:

                startActivity(new Intent(this, MyInComeActivity.class));

                break;
            case R.id.center_setting_rl:

                startActivity(new Intent(this, SettingActivity.class));

                break;
            case R.id.center_my_cast_rl:

                startActivity(new Intent(this, MyCastActivity.class));

                break;
            case R.id.center_my_order_rl:

                startActivity(new Intent(this, OrderActivity.class));

                break;
            case R.id.center_message_notify_rl:

                startActivity(new Intent(this, MessageCenterActivity.class));

                break;
            case R.id.center_vip_privileges_rl:

                startActivity(new Intent(this, VipActivity.class));

                break;
            case R.id.center_logout_rl:

                new LogoutDialog(this) {
                    @Override
                    public void onConfirm() {

                        String uid = BaseApplication.BasePreferences.readUserId();

                        BaseApplication.BasePreferences.clear();
                        BaseApplication.BasePreferences.saveIsGuide(true);
                        BaseApplication.BasePreferences.saveIsLogin(false);
                        BaseApplication.BasePreferences.saveLogoutUserId(uid);

                        startActivity(new Intent(BusinessCenterActivity.this, LoginActivity.class));

                        BaseApplication.INSTANCE.finishActivity(HomeActivity.class, BusinessCenterActivity.class);
                    }
                }.show();

                break;
        }

    }

    public class RefreshTitleAndHead implements AppCallBack {

        public void refreshAll(String title, String url) {

            setRightName(title, url, R.id.default_id, BusinessCenterActivity.this);

        }

        public void refreshHead(String url) {

            setRightName(null, url, R.id.default_id, BusinessCenterActivity.this);

        }

        public void refreshMessage(int newsCount) {

            if (BaseApplication.BasePreferences.readMessageCount() < newsCount) {

                center_message_red_iv.setVisibility(View.VISIBLE);


            } else {

                center_message_red_iv.setVisibility(View.GONE);

            }

        }

        public void refresh () {

            bussnessCenterInfoPost.sid = BaseApplication.BasePreferences.readShopId();
            bussnessCenterInfoPost.execute(BusinessCenterActivity.this);
        }

    }

}
